package ch.sfug.player {
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	import ch.sfug.events.PlayerEvent;
	import ch.sfug.events.TimeProgressEvent;
	import ch.sfug.player.AbstractPlayer;		

	/**
	 * @author mich
	 */
	public class PlayState extends AbstractPlayerState {

		private var progress:Timer;

		public function PlayState( player:AbstractPlayer ) {
			super( player);
			progress = new Timer( 200);
			progress.addEventListener( TimerEvent.TIMER, dispatchProgress);
			progress.start();
		}

		
		private function dispatchProgress( event:TimerEvent = null ):void {
			if( !isNaN( player.time ) && !isNaN( player.duration ) ) {
				player.dispatchEvent( new TimeProgressEvent( TimeProgressEvent.TIME_PROGRESS, player.time, player.duration ) );
			}
		}

		
		public override function pause():void {
			player.pausePlayer();
			player.setState( new PauseState( player ) );
		}
		
		
		public override function stop():void {
			player.stopPlayer();
			player.setState( new StopState(player ) );
		}
		
		override public function end():void {
			player.stopPlayer();
			super.end();
		}
		
		
		public override function play():void {
			// nothing to do already in play state
		}

		
		public override function dispatchState( ):void {
			player.dispatchEvent( new PlayerEvent( PlayerEvent.PLAY ) );
		}

		
		public override function isPlaying():Boolean {
			return true;
		}
		
		/**
		 * stop the timer to dispatch time progress
		 */
		internal override function destroy():void {
			progress.stop();
			dispatchProgress();
			progress = null;
			super.destroy();
		}
	}
}
